﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UtilLib
{
    public static class MathUtil
    {
        public static String ConvertToFinancialFormat(String numText)
        {
            Decimal numValue;

            //If numText is a number, return it directly.
            if (Decimal.TryParse(numText, out numValue)) return numValue.ToString();

            //If numText ends with "m", parse it in a recursive way.
            if (numText.EndsWith("m", StringComparison.OrdinalIgnoreCase) && Decimal.TryParse(ConvertToFinancialFormat(numText.Substring(0, numText.Length - 1)), out numValue))
                return Convert.ToInt64(numValue * 1000).ToString();
            else if (numText.EndsWith("bb", StringComparison.OrdinalIgnoreCase) && Decimal.TryParse(ConvertToFinancialFormat(numText.Substring(0, numText.Length - 2)), out numValue))
                return Convert.ToInt64(numValue * 1000000000).ToString();
            //If numText doesn't end with "m" and can't be converted to decimal, then return text directly.
            return numText;
        }

        public static String ConvertToFinancialFormat(String numText, String oldValue)
        {
            String[] arrValue;
            Decimal temValue = 0;
            Decimal sumValue = 0;
            if (numText.IndexOf("+") != -1)
            {
                arrValue = numText.Split(new String[] { "+" }, StringSplitOptions.None);
                for (int i = 0; i < arrValue.Length; i++)
                {
                    arrValue[0] = numText.StartsWith("+") ? oldValue : arrValue[0];
                    if (!Decimal.TryParse(ConvertToFinancialFormat(arrValue[i]), out temValue))
                    {
                        if (!Decimal.TryParse(ConvertToFinancialFormat(arrValue[i], oldValue), out temValue))
                            return numText;
                    }
                    sumValue += temValue;
                }
                return sumValue.ToString();
            }

            if (numText.IndexOf("-") != -1)
            {
                arrValue = numText.Split(new String[] { "-" }, StringSplitOptions.None);
                for (int i = 0; i < arrValue.Length; i++)
                {
                    arrValue[0] = numText.StartsWith("-") ? oldValue : arrValue[0];
                    if (!Decimal.TryParse(ConvertToFinancialFormat(arrValue[i]), out temValue))
                    {
                        if (!Decimal.TryParse(ConvertToFinancialFormat(arrValue[i], oldValue), out temValue))
                            return numText;
                    }
                    sumValue = i == 0 ? temValue : sumValue - temValue;
                }
                return sumValue.ToString();
            }
            return ConvertToFinancialFormat(numText);
        }
        public static string ConvertDoubleToStr(Double amount)
        {
            string amStr = string.Empty;
            if (amount != 0)
            {
                if (amount % 1000000000 == 0)
                    amStr = (amount / (double)1000000000).ToString("#,0") + "BB";
                else if (amount % 1000000 == 0)
                    amStr = (amount / (double)1000000).ToString("#,0") + "MM";
                else if (amount % 1000 == 0)
                    amStr = (amount / (double)1000).ToString("#,0") + "M";
                else
                    amStr = amount.ToString("#,0");
            }
            return amStr;
        }
    }
}
